package com.example.usagelock;

import java.util.LinkedList;
import java.util.Queue;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.surfaceball.R;

public class ConfigActivity extends Activity {
	LinearLayout passwordLayout;
	Queue<Integer> queue;
	ImageManager imageManager;
	PasswordOrganizer passwordManager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.config_main);
		passwordLayout = (LinearLayout)findViewById(R.id.password);
		
		queue = new LinkedList<Integer>();
		imageManager = ImageManager.getInstance(this);
		passwordManager = new PasswordOrganizer(this);
	}
	
	private ImageButton createImageButton(int imageType){
		ImageButton btn = new ImageButton(getApplicationContext());
		btn.setImageBitmap(imageManager.getImage(imageType, ImageStyle.Normal));
		return btn;
	}
	
	public void onClickButton(View view){
		int id = view.getId();
		if(id == R.id.cancelBtn){
			finish();
		}else if(id == R.id.saveBtn){
			int index = 0;
			passwordManager.setLength(queue.size());
			while(!queue.isEmpty()){
				passwordManager.setPasswrod(index, queue.poll());
				index++;
			}
			
			finish();
		}
	}
	
	public void onClickImageButton(View view){
		int imageType = -1;
		switch(view.getId()){
		case R.id.keyBtn:
			imageType = ImageType.Key;
			break;
		case R.id.glassesBtn:
			imageType = ImageType.Glasses;
			break;
		case R.id.heartBtn:
			imageType = ImageType.Heart;
			break;
		case R.id.memoBtn:
			imageType = ImageType.Memo;
			break;
		case R.id.noteBtn:
			imageType = ImageType.Note;
			break;
		case R.id.pencilBtn:
			imageType = ImageType.Pencil;
			break;
		case R.id.puzzleBtn:
			imageType = ImageType.Puzzle;
			break;
		case R.id.thunderBtn:
			imageType = ImageType.Thunder;
			break;
		}
		
		if(imageType != -1){
			passwordLayout.addView(createImageButton(imageType));
			queue.add(imageType);
		}
	}
}
